#include ".\sprite.h"
#pragma  comment(lib,"Msimg32.lib")
Sprite::Sprite(char *background, char *bmp, COLORREF TColor, int row, int col)
{
	this->row = row;
	this->col = col;
	this->backbmp = (HBITMAP)LoadImage(NULL,background,IMAGE_BITMAP,0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
	this->bmp = (HBITMAP)LoadImageA(NULL,bmp,IMAGE_BITMAP,0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
	GetObject(this->bmp,sizeof(tempbmp),&tempbmp);
	this->bmpwidth = tempbmp.bmWidth/row;
	this->bmpheight = tempbmp.bmHeight/col;
	GetObject(this->backbmp,sizeof(btempbmp),&btempbmp);
	this->TColor = TColor;
}

Sprite::~Sprite(void)
{
	DeleteObject(bmp);
	DeleteObject(backbmp);
}

void Sprite::Show(HDC hdc,int bmpx, int bmpy, int bmpvx, int bmpvy)
{
	HDC mdc,tempdc;
	mdc = CreateCompatibleDC(hdc);
	tempdc = CreateCompatibleDC(hdc);
	HBITMAP bbmp= CreateCompatibleBitmap(hdc,btempbmp.bmWidth,btempbmp.bmHeight);
	SelectObject(mdc,bbmp);
	if(bmpvx>0)
	{
		for(int i=0;i<=row;i++)
		{
			DWORD w = GetTickCount();
			SelectObject(tempdc,this->backbmp);
			BitBlt(mdc,0,0,btempbmp.bmWidth,btempbmp.bmHeight,tempdc,0,0,SRCCOPY);
			SelectObject(tempdc,this->bmp);
			if(i==row)
			{
				TransparentBlt(mdc,bmpx,bmpy,this->bmpwidth,this->bmpheight,tempdc,0,2*this->bmpheight,this->bmpwidth,this->bmpheight,this->TColor);
			}
			else
			{
				TransparentBlt(mdc,bmpx,bmpy,this->bmpwidth,this->bmpheight,tempdc,i*this->bmpwidth,2*this->bmpheight,this->bmpwidth,this->bmpheight,this->TColor);
			}
			BitBlt(hdc,0,0,this->btempbmp.bmWidth,this->btempbmp.bmHeight,mdc,0,0,SRCCOPY);
			while(GetTickCount() - w <= 30);
			bmpx += bmpvx;
		}
	}
	else if(bmpvx<0)
	{
		for(int i=0;i<=row;i++)
		{
			DWORD w = GetTickCount();
			SelectObject(tempdc,this->backbmp);
			BitBlt(mdc,0,0,btempbmp.bmWidth,btempbmp.bmHeight,tempdc,0,0,SRCCOPY);
			SelectObject(tempdc,this->bmp);
			if(i==row)
			{
				TransparentBlt(mdc,bmpx,bmpy,this->bmpwidth,this->bmpheight,tempdc,0,1*this->bmpheight,this->bmpwidth,this->bmpheight,this->TColor);
			}
			else
			{
				TransparentBlt(mdc,bmpx,bmpy,this->bmpwidth,this->bmpheight,tempdc,i*this->bmpwidth,1*this->bmpheight,this->bmpwidth,this->bmpheight,this->TColor);
			}
			BitBlt(hdc,0,0,this->btempbmp.bmWidth,this->btempbmp.bmHeight,mdc,0,0,SRCCOPY);
			while(GetTickCount() - w <= 30);
			bmpx += bmpvx;
		}
	}
	if(bmpvy>0)
	{
		for(int i=0;i<=row;i++)
		{
			DWORD w = GetTickCount();
			SelectObject(tempdc,this->backbmp);
			BitBlt(mdc,0,0,btempbmp.bmWidth,btempbmp.bmHeight,tempdc,0,0,SRCCOPY);
			SelectObject(tempdc,this->bmp);
			if(i==row)
			{
				TransparentBlt(mdc,bmpx,bmpy,this->bmpwidth,this->bmpheight,tempdc,0,0,this->bmpwidth,this->bmpheight,this->TColor);
			}
			else
			{
				TransparentBlt(mdc,bmpx,bmpy,this->bmpwidth,this->bmpheight,tempdc,i*this->bmpwidth,0,this->bmpwidth,this->bmpheight,this->TColor);
			}
			BitBlt(hdc,0,0,this->btempbmp.bmWidth,this->btempbmp.bmHeight,mdc,0,0,SRCCOPY);
			while(GetTickCount() - w <= 30);
			bmpy += bmpvy;
		}
	}
	else if(bmpvy<0)
	{
		for(int i=0;i<=row;i++)
		{
			DWORD w = GetTickCount();
			SelectObject(tempdc,this->backbmp);
			BitBlt(mdc,0,0,btempbmp.bmWidth,btempbmp.bmHeight,tempdc,0,0,SRCCOPY);
			SelectObject(tempdc,this->bmp);
			if(i==row)
			{
				TransparentBlt(mdc,bmpx,bmpy,this->bmpwidth,this->bmpheight,tempdc,0,3*this->bmpheight,this->bmpwidth,this->bmpheight,this->TColor);
			}
			else
			{
				TransparentBlt(mdc,bmpx,bmpy,this->bmpwidth,this->bmpheight,tempdc,i*this->bmpwidth,3*this->bmpheight,this->bmpwidth,this->bmpheight,this->TColor);
			}
			BitBlt(hdc,0,0,this->btempbmp.bmWidth,this->btempbmp.bmHeight,mdc,0,0,SRCCOPY);
			while(GetTickCount() - w <= 30);
			bmpy += bmpvy;
		}
	}
	if(bmpvx == 0 && bmpvy==0)
	{
		for(int i=0;i<=row;i++)
		{
			DWORD w = GetTickCount();
			SelectObject(tempdc,this->backbmp);
			BitBlt(mdc,0,0,btempbmp.bmWidth,btempbmp.bmHeight,tempdc,0,0,SRCCOPY);
			SelectObject(tempdc,this->bmp);
			if(i==row)
			{
				TransparentBlt(mdc,bmpx,bmpy,this->bmpwidth,this->bmpheight,tempdc,0,0*this->bmpheight,this->bmpwidth,this->bmpheight,this->TColor);
			}
			else
			{
				TransparentBlt(mdc,bmpx,bmpy,this->bmpwidth,this->bmpheight,tempdc,i*this->bmpwidth,0*this->bmpheight,this->bmpwidth,this->bmpheight,this->TColor);
			}
			BitBlt(hdc,0,0,this->btempbmp.bmWidth,this->btempbmp.bmHeight,mdc,0,0,SRCCOPY);
			while(GetTickCount() - w <= 30);
		}

	}
	DeleteObject(mdc);
	DeleteObject(tempdc);
}
